
/* eslint-disable no-undef */
// const globalUrl = 'http://47.97.98.33/'

function parseList (list, ListId, ParentId, root) {
    ListId = ListId || 'ListItemId'
    ParentId = ParentId || 'ParentId'
    root = root || '-1'
    var map = {}
    list.forEach(function (item) {
        if (item.isLeaf) {
            item.hasChildren = !item.isLeaf
        }
        if(!map[item[ListId]]) {
            map[item[ListId]] = item
        }
    })
    list.forEach(function (item) {
        map[item[ParentId]] = map[item[ParentId]] || {}
        map[item[ParentId]].children ? map[item[ParentId]].children.push(item) : map[item[ParentId]].children = [item]
    })
    if (!(root in map)) {
        map[root] = {
            children: []
        }
    }
    return map[root].children
}
function debounce (handle, wait, immediate) {
    if (typeof handle !== 'function') throw new Error('handle muse be function')
    if (typeof wait === 'undefined') wait = 300
    if (typeof wait === 'boolean') {
        immediate = wait
        wait = 300
    }
    if (typeof immediate !== 'boolean') immediate = false
    let timer = null
    return function proxy (...args) {
        const me = this,
            init = immediate && !timer
        clearTimeout(timer)
        timer = setTimeout(() => {
            timer = null
            !immediate ? handle.call(me, ...args) : null
        }, wait)
        init ? handle.call(me, ...args) : null
    }
}

export {
    parseList,
    debounce
}

